package com.rong.action;

import com.opensymphony.xwork2.ActionSupport;
import com.rong.hibernate.BaseHibernateDAO;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.rong.DAO.ZynCarDAO;
import com.rong.DAO.ZynPrice34DAO;
import com.rong.ORM.ZynCar;
import com.rong.ORM.ZynPrice34;

import com.rong.DAO.ZynCompanyDAO;
import com.rong.ORM.ZynCompany;


public class ComputeAllAction extends ActionSupport  {
	
	public int comIdA=-1;
	public int comIdB=-1;
	public int carId=-1;
	public String stmtStyle="-1";
	public String result="";
	public String which="";
	
	public String giveHeatStyle="";
	
	
	 /**
	 * @return the giveHeatStyle
	 */
	public String getGiveHeatStyle() {
		return giveHeatStyle;
	}

	/**
	 * @param giveHeatStyle the giveHeatStyle to set
	 */
	public void setGiveHeatStyle(String giveHeatStyle) {
		this.giveHeatStyle = giveHeatStyle;
	}

	/**
	 * @return the which
	 */
	public String getWhich() {
		return which;
	}

	/**
	 * @param which the which to set
	 */
	public void setWhich(String which) {
		this.which = which;
	}

	/**
	 * @return the result
	 */
	public String getResult() {
		return result;
	}

	/**
	 * @param result the result to set
	 */
	public void setResult(String result) {
		this.result = result;
	}

	/**
	 * @return the comIdA
	 */
	public int getComIdA() {
		return comIdA;
	}

	/**
	 * @param comIdA the comIdA to set
	 */
	public void setComIdA(int comIdA) {
		this.comIdA = comIdA;
	}

	/**
	 * @return the comIdB
	 */
	public int getComIdB() {
		return comIdB;
	}

	/**
	 * @param comIdB the comIdB to set
	 */
	public void setComIdB(int comIdB) {
		this.comIdB = comIdB;
	}

	/**
	 * @return the carId
	 */
	public int getCarId() {
		return carId;
	}

	/**
	 * @param carId the carId to set
	 */
	public void setCarId(int carId) {
		this.carId = carId;
	}

	

	/**
	 * @return the stmtStyle
	 */
	public String getStmtStyle() {
		return stmtStyle;
	}

	/**
	 * @param stmtStyle the stmtStyle to set
	 */
	public void setStmtStyle(String stmtStyle) {
		this.stmtStyle = stmtStyle;
	}



	/**
	 * 
	 * fromID: 采热地点
	 * toID：客户ID
	 * carID：车辆ID
	 * stmtStyle:结算方式ID
	 * 
	 */

	BaseHibernateDAO b=new BaseHibernateDAO();
	
	 @Override
	public String execute()
	    throws ServletException, IOException {
		 
		 ZynPrice34DAO d=new ZynPrice34DAO();
		 ZynCompanyDAO comDao=new ZynCompanyDAO();
		ZynCarDAO carDao=new ZynCarDAO();
			
	        HttpServletResponse response = ServletActionContext.getResponse()   ;
            PrintWriter out=response.getWriter();
         
	        Session s=null;
	        try {
	        s=b.getSession();
	       
		        if(this.comIdA>0&this.comIdB>0&this.carId>0&!(this.stmtStyle.equalsIgnoreCase("-1"))){ //条件满足才返回值
			      
		        	 Criteria c=s.createCriteria(ZynPrice34.class);
		        	 
		 	         c.add(Restrictions.eq("comIdA", this.getComIdA()));
		 	         c.add(Restrictions.eq("comIdB", this.getComIdB()));
		 	         c.add(Restrictions.eq("carId", this.getCarId()));
		 	         
		 	         if(this.getWhich().equalsIgnoreCase("t")){  //transfer price
		 	        	 c.add(Restrictions.eq("stmtStyleTrafc", this.getStmtStyle()));
		 	         }else{											// sell price
		 	        	 c.add(Restrictions.eq("stmtStyleSell", this.getStmtStyle()));	 	
		 	        	 c.add(Restrictions.eq("extAttr1", this.getGiveHeatStyle()));	 	//销售 价格定义中的 供热类型
		 	         }
		 	      
		 	         c.add(Restrictions.or(Restrictions.isNull("invalidationDate"),Restrictions.gt("invalidationDate", new Date()))); //日期为空或大于当下日期
		 	       
		        	
		        	List<ZynPrice34> list=c.list();	
		        	
		        	if(list.size()>0){
				        	ZynPrice34 p34=(ZynPrice34)list.get(0);			        
				        	this.setResult(p34.getId()+"t"+p34.getPriceTrafc()+"p"+p34.getPriceSell());
		        	}else{//没有对应记录，返回0		        		
		        			this.setResult("-1t0p0");
		        	}
		        	
		        	
		        	// ============= 用热客户的 放热温度 ============
		        	
		        	this.setResult(this.result+"c"+(comDao.findById(this.comIdB).getExtAttr2()==null?0:comDao.findById(this.comIdB).getExtAttr2()));
		        	//============= 用热客户的 放热温度 ==============
		        	
		        	// =============车辆容积 ============
		        	
		        	this.setResult(this.result+"v"+(carDao.findById(this.carId).getExtAttr3()==null?0:carDao.findById(this.carId).getExtAttr3()));
		        	//=============车辆容积 ==============
		        	
		        	// =============车辆标准放热量 ============
		        	
		        	this.setResult(this.result+"h"+(carDao.findById(this.carId).getExtAttr5()==null?0:carDao.findById(this.carId).getExtAttr5()));
		        	//=============车辆标准放热量  ==============
		        }
            
           
            
	        }
	        catch(Exception pe) {
	        	 out.println("-2t0p0c0v0h0");   
	             out.close();
	        	System.out.println("[Compute Action]..erro.]");
	        	System.out.println("ParseException: " + pe.toString());
	        }finally{
	        	 out.println(this.result);  
	             out.close();
	        	 return "SUCCESS";
	        }
	      
	    }
}
